﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Dapper.Database;
using Dapper.Database.Extensions;
namespace QuickFramework.Orm
{
    public partial class Repository
    {
        #region Dapper 扩展方法
        public int Execute(string sql, object param = null, IUnitOfWork unitOfWork = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            if (unitOfWork != null)
            {
                return unitOfWork.Connection.Execute(sql, param, unitOfWork.Transaction, commandTimeout, commandType);
            }
            else
            {
                using (var uow = UnitOfWork)
                {
                    return uow.Connection.Execute(sql, param, unitOfWork.Transaction, commandTimeout, commandType);
                }
            }
        }
        //
        // 摘要:
        //     Execute a command asynchronously using Task.
        //
        // 参数:
        //   cnn:
        //     The connection to query on.
        //
        //   sql:
        //     The SQL to execute for this query.
        //
        //   param:
        //     The parameters to use for this query.
        //
        //   transaction:
        //     The transaction to use for this query.
        //
        //   commandTimeout:
        //     Number of seconds before command execution timeout.
        //
        //   commandType:
        //     Is it a stored proc or a batch?
        //
        // 返回结果:
        //     The number of rows affected.
        public Task<int> ExecuteAsync(string sql, object param = null, IUnitOfWork unitOfWork = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            if (unitOfWork != null)
            {
                return unitOfWork.Connection.ExecuteAsync(sql, param, unitOfWork.Transaction, commandTimeout, commandType);
            }
            else
            {
                using (var uow = UnitOfWork)
                {
                    return uow.Connection.ExecuteAsync(sql, param, unitOfWork.Transaction, commandTimeout, commandType);
                }
            }
        }
        //
        // 摘要:
        //     Execute parameterized SQL and return an System.Data.IDataReader.
        //
        // 参数:
        //   cnn:
        //     The connection to execute on.
        //
        //   sql:
        //     The SQL to execute.
        //
        //   param:
        //     The parameters to use for this command.
        //
        //   transaction:
        //     The transaction to use for this command.
        //
        //   commandTimeout:
        //     Number of seconds before command execution timeout.
        //
        //   commandType:
        //     Is it a stored proc or a batch?
        //
        // 返回结果:
        //     An System.Data.IDataReader that can be used to iterate over the results of the
        //     SQL query.
        //
        // 言论：
        //     This is typically used when the results of a query are not processed by Dapper,
        //     for example, used to fill a System.Data.DataTable or DataSet.
        public IDataReader ExecuteReader(string sql, object param = null, IUnitOfWork unitOfWork = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            if (unitOfWork != null)
            {
                return unitOfWork.Connection.ExecuteReader(sql, param, unitOfWork.Transaction, commandTimeout, commandType);
            }
            else
            {
                using (var uow = UnitOfWork)
                {
                    return uow.Connection.ExecuteReader(sql, param, unitOfWork.Transaction, commandTimeout, commandType);
                }
            }
        }
        //
        // 摘要:
        //     Execute parameterized SQL and return a System.Data.Common.DbDataReader.
        //
        // 参数:
        //   cnn:
        //     The connection to execute on.
        //
        //   sql:
        //     The SQL to execute.
        //
        //   param:
        //     The parameters to use for this command.
        //
        //   transaction:
        //     The transaction to use for this command.
        //
        //   commandTimeout:
        //     Number of seconds before command execution timeout.
        //
        //   commandType:
        //     Is it a stored proc or a batch?
        public Task<IDataReader> ExecuteReaderAsync(string sql, object param = null, IUnitOfWork unitOfWork = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            if (unitOfWork != null)
            {
                return unitOfWork.Connection.ExecuteReaderAsync(sql, param, unitOfWork.Transaction, commandTimeout, commandType);
            }
            else
            {
                using (var uow = UnitOfWork)
                {
                    return uow.Connection.ExecuteReaderAsync(sql, param, unitOfWork.Transaction, commandTimeout, commandType);
                }
            }
        }
        //
        // 摘要:
        //     Execute parameterized SQL that selects a single value.
        //
        // 参数:
        //   cnn:
        //     The connection to execute on.
        //
        //   sql:
        //     The SQL to execute.
        //
        //   param:
        //     The parameters to use for this command.
        //
        //   transaction:
        //     The transaction to use for this command.
        //
        //   commandTimeout:
        //     Number of seconds before command execution timeout.
        //
        //   commandType:
        //     Is it a stored proc or a batch?
        //
        // 返回结果:
        //     The first cell selected as System.Object.
        public object ExecuteScalar(string sql, object param = null, IUnitOfWork unitOfWork = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            if (unitOfWork != null)
            {
                return unitOfWork.Connection.ExecuteScalar(sql, param, unitOfWork.Transaction, commandTimeout, commandType);
            }
            else
            {
                using (var uow = UnitOfWork)
                {
                    return uow.Connection.ExecuteScalar(sql, param, unitOfWork.Transaction, commandTimeout, commandType);
                }
            }
        }
        //
        // 摘要:
        //     Execute parameterized SQL that selects a single value.
        //
        // 参数:
        //   cnn:
        //     The connection to execute on.
        //
        //   sql:
        //     The SQL to execute.
        //
        //   param:
        //     The parameters to use for this command.
        //
        //   transaction:
        //     The transaction to use for this command.
        //
        //   commandTimeout:
        //     Number of seconds before command execution timeout.
        //
        //   commandType:
        //     Is it a stored proc or a batch?
        //
        // 类型参数:
        //   T:
        //     The type to return.
        //
        // 返回结果:
        //     The first cell returned, as T.
        public T ExecuteScalar<T>(string sql, object param = null, IUnitOfWork unitOfWork = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            if (unitOfWork != null)
            {
                return unitOfWork.Connection.ExecuteScalar<T>(sql, param, unitOfWork.Transaction, commandTimeout, commandType);
            }
            else
            {
                using (var uow = UnitOfWork)
                {
                    return uow.Connection.ExecuteScalar<T>(sql, param, unitOfWork.Transaction, commandTimeout, commandType);
                }
            }
        }
        // 摘要:
        //     Execute parameterized SQL that selects a single value.
        //
        // 参数:
        //   cnn:
        //     The connection to execute on.
        //
        //   sql:
        //     The SQL to execute.
        //
        //   param:
        //     The parameters to use for this command.
        //
        //   transaction:
        //     The transaction to use for this command.
        //
        //   commandTimeout:
        //     Number of seconds before command execution timeout.
        //
        //   commandType:
        //     Is it a stored proc or a batch?
        //
        // 类型参数:
        //   T:
        //     The type to return.
        //
        // 返回结果:
        //     The first cell returned, as T.
        public Task<T> ExecuteScalarAsync<T>(string sql, object param = null, IUnitOfWork unitOfWork = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            if (unitOfWork != null)
            {
                return unitOfWork.Connection.ExecuteScalarAsync<T>(sql, param, unitOfWork.Transaction, commandTimeout, commandType);
            }
            else
            {
                using (var uow = UnitOfWork)
                {
                    return uow.Connection.ExecuteScalarAsync<T>(sql, param, unitOfWork.Transaction, commandTimeout, commandType);
                }
            }
        }
        //
        // 摘要:
        //     Execute parameterized SQL that selects a single value.
        //
        // 参数:
        //   cnn:
        //     The connection to execute on.
        //
        //   sql:
        //     The SQL to execute.
        //
        //   param:
        //     The parameters to use for this command.
        //
        //   transaction:
        //     The transaction to use for this command.
        //
        //   commandTimeout:
        //     Number of seconds before command execution timeout.
        //
        //   commandType:
        //     Is it a stored proc or a batch?
        //
        // 返回结果:
        //     The first cell returned, as System.Object.
        public Task<object> ExecuteScalarAsync(string sql, object param = null, IUnitOfWork unitOfWork = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            if (unitOfWork != null)
            {
                return unitOfWork.Connection.ExecuteScalarAsync(sql, param, unitOfWork.Transaction, commandTimeout, commandType);
            }
            else
            {
                using (var uow = UnitOfWork)
                {
                    return uow.Connection.ExecuteScalarAsync(sql, param, unitOfWork.Transaction, commandTimeout, commandType);
                }
            }
        }

        #endregion
    }
}
